#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson

from model import StudentVoice
from model import ClassState
from model import Lecture

class MyHandler(webapp.RequestHandler):
    
    def get(self):
#        voiceCount = [];
       
        lectureKey=self.request.get('lectureKey')
        lecture=Lecture.get(lectureKey)
        
        classState=db.Query(ClassState).filter('lecture = ', lecture).get()
        currTopic=classState.currentTopicKey
        
        voices = db.Query(StudentVoice).filter('lecture = ', lecture).filter('topic = ', currTopic)
        #.filter('Understand = ', False).count()
        voiceCount = [voices.count(),voices.filter('Understand = ', False).count()]
            
        # change! - modify by class id.  
        numOfStuds=db.Query(StudentVoice).count()
            
        # change! - get real subjects
        classState=db.Query(ClassState)
#        db.delete(classState)
        classState=classState.get()
        
        subjectIndex=1
        isQuizOn = False
        quizQuestion = None

        if classState:
            subjectKey = str(classState.currentTopicKey.key())
            isQuizOn = classState.isQuizOn
            quizQuestion = classState.quizQuestionKey


        dirResponse=dict(studentCols=voiceCount,numOfStudents=numOfStuds,subjectInd=subjectKey, isQuizOn=isQuizOn, quizKey = str(quizQuestion.key()));
        
        self.response.out.write(simplejson.dumps(dirResponse))                        

def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

##!/usr/bin/env python
#
#import wsgiref.handlers
#from google.appengine.ext import db
#from google.appengine.ext import webapp
#from google.appengine.ext.webapp import template
#from django.utils import simplejson
#import datetime
#from model import Voice
#
#class MyHandler(webapp.RequestHandler):
#    def get(self):
#        self.response.out.write("test");
#        self.
##        self.response.out.write("json page works");
#        voiceCount = ();
#        for i in range(10):
#            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
#            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
#            voiceCount += (simplejson.loads(
#                        db.Query(Voice)
#                        .filter('when > ', plus_one_minutes_ago)
#                        .filter('when < ', x_minutes_ago)
#                        .count()) , );
##                        
##        
##        #self.response.out.write(simplejson.dumps(voiceCount));